package com.module.device.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import base.util.datetime.DateTimeUtil;

import com.activityClient.util.PropertiesUtil;
import com.module.TraceSource.manager.TsEntEventManager;
import com.module.TraceSource.manager.TsEntFlowManager;
import com.module.TraceSource.vo.TsEntEvent;
import com.module.TraceSource.vo.TsEntFlow;
import com.module.device.manager.DeviceActiveStatusManager;
import com.module.device.vo.DeviceActiveStatus;
import com.module.device.vo.DeviceRunDetail;
import com.module.sys.LoginSession;

/**
 * 查看设备运行情况类
 * 
 * @author allen
 * 
 */
public class DeviceRuntimeService {
	DeviceActiveStatusManager devicemanager = DeviceActiveStatusManager
			.getInstance();
	TsEntFlowManager flowmanager = TsEntFlowManager.getInstance();
	TsEntEventManager eventmanager = TsEntEventManager.getInstance();

	/**
	 * 根据设备ID获取运行结果
	 * 
	 * @return
	 * @throws Exception
	 */
	public boolean getRunInfoById(String softwareCode) throws Exception {
		boolean statu = false;

		DeviceActiveStatus statuvo = devicemanager.getRunInfoById(softwareCode);

		if (statuvo != null
				&& statuvo.getActiveStatus().toUpperCase().equals("A")) {
			statu = true;
		}

		return statu;
	}

	/**
	 * 根据事件ID获取设备运行结果
	 * 
	 * @return
	 * @throws Exception
	 */
	public List<DeviceRunDetail> getRunInfoByEventId(int eventId)
			throws Exception {
		List<DeviceRunDetail> detailList = new ArrayList<DeviceRunDetail>();

		List list = devicemanager.getRunInfoByEventId(eventId);
		for (int i = 0; i < list.size(); i++) {
			Object[] obj = (Object[]) list.get(i);

			DeviceRunDetail vo = new DeviceRunDetail();

			vo.setDeviceTypeName((String) obj[0]);
			vo.setDeviceCode((String) obj[1]);
			vo.setSoftwareCode((String) obj[2]);
			vo.setActiveStatus(getDeviceStatu(vo.getSoftwareCode()));
			if (obj[4] != null)
				vo.setCreatetime(DateTimeUtil.parseUtilDateTime(obj[4]
						.toString()));
			
			System.out.println(vo.getCreatetime().toString()+".........................获取时间"+obj[4]
					.toString());
			vo.setUserTruename(String.valueOf(obj[5]));
			vo.setEventName((String) obj[6]);

			detailList.add(vo);
		}
		return detailList;
	}

	/**
	 * 获取设备状态
	 * 
	 * @param softwareCode
	 * @return
	 * @throws Exception
	 */
	public String getDeviceStatu(String softwareCode) throws Exception {
		DeviceActiveStatus bean = DeviceActiveStatusManager.getInstance()
				.getRunInfoById(softwareCode);
		if (bean != null) {
			int activetime = Integer.parseInt(PropertiesUtil
					.getString("activetime"));
			float isActive = (System.currentTimeMillis() - bean.getUpdatetime()
					.getTime()) / 60000;
			System.out.println("isActive=" + isActive);
			if (isActive < activetime) {
				return "活跃";
			} else {
				return "不活跃";
			}
		} else {
			return "不活跃";
		}
	}

	/**
	 * 获取流程
	 * 
	 * @return
	 * @throws Exception
	 */
	public List<TsEntFlow> getFlow() throws Exception {
		String downloadPath = PropertiesUtil.getString("downloadPath");
		String downloadHostName = PropertiesUtil.getString("downloadHostName");
		int downloadPort = Integer.parseInt(PropertiesUtil
				.getString("downloadPort"));

		String orgid = LoginSession.getOrgid();
		List<TsEntFlow> flowlist = flowmanager.findEntFlowByOrgId(orgid);
		for (TsEntFlow ee : flowlist) {
			String imgUrl = "http://" + downloadHostName + ":" + downloadPort
					+ downloadPath + ee.getImgUrl();
			ee.setImgUrl(imgUrl);
		}
		return flowlist;
	}

	/**
	 * 获取事件
	 * 
	 * @param flowId
	 * @return
	 * @throws Exception
	 */
	public List<TsEntEvent> getEvent(String flowId) throws Exception {
		String downloadPath = PropertiesUtil.getString("downloadPath");
		String downloadHostName = PropertiesUtil.getString("downloadHostName");
		int downloadPort = Integer.parseInt(PropertiesUtil
				.getString("downloadPort"));

		List<TsEntEvent> list = eventmanager.findEntEventByFlowId(flowId);
		for (int i = 0; i < list.size(); i++) {
			TsEntEvent vo = list.get(i);
			if (i == list.size() - 1) {
				vo.setImgUrl2("imgResources/sprites2.png");
			} else {
				vo.setImgUrl2("imgResources/feature_right.gif");
			}

			String imgUrl = "http://" + downloadHostName + ":" + downloadPort
					+ downloadPath + vo.getImgUrl();
			vo.setImgUrl(imgUrl);
		}
		return list;
	}

	public String getEventById(int eventId) {
		String eventName = "";
		TsEntEvent vo = eventmanager.getEventById(eventId);
		if (vo != null)
			eventName = vo.getEventName();
		return eventName;
	}
}
